home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJLSR106.ARJ / DIV.C < prev    next >
C/C++ Source or Header  |  1992-03-02  |  2KB  |  65 lines

  1. /* This file may have been modified by DJ Delorie (Jan 1991).  If so,
  2. ** these modifications are Coyright (C) 1991 DJ Delorie, 24 Kirsten Ave,
  3. ** Rochester NH, 03867-2954, USA.
  4. */
  5.  
  6. /*
  7.  * Copyright (c) 1990 Regents of the University of California.
  8.  * All rights reserved.
  9.  *
  10.  * This code is derived from software contributed to Berkeley by
  11.  * Chris Torek.
  12.  *
  13.  * Redistribution and use in source and binary forms are permitted
  14.  * provided that: (1) source distributions retain this entire copyright
  15.  * notice and comment, and (2) distributions including binaries display
  16.  * the following acknowledgement:  ``This product includes software
  17.  * developed by the University of California, Berkeley and its contributors''
  18.  * in the documentation or other materials provided with the distribution
  19.  * and in all advertising materials mentioning features or use of this
  20.  * software. Neither the name of the University nor the names of its
  21.  * contributors may be used to endorse or promote products derived
  22.  * from this software without specific prior written permission.
  23.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  24.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  25.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  26.  */
  27.  
  28. #if defined(LIBC_SCCS) && !defined(lint)
  29. static char sccsid[] = "@(#)div.c    5.1 (Berkeley) 5/16/90";
  30. #endif /* LIBC_SCCS and not lint */
  31.  
  32. #include <stdlib.h>        /* div_t */
  33.  
  34. /*
  35.  * I AM NOT SURE THIS IS COMPLETELY PORTABLE
  36.  * (or that it is even right)
  37.  */
  38. div_t
  39. div(num, denom)
  40.     int num, denom;
  41. {
  42.     div_t r;
  43.  
  44.     /* avoid deep thought */
  45.     if (num > 0 && denom < 0) {
  46.         num = -num;
  47.         denom = -denom;
  48.     }
  49.     r.quot = num / denom;
  50.     r.rem = num % denom;
  51.     if (num < 0 && denom > 0) {
  52.         /*
  53.          * Machine division and remainer may work either way.  The
  54.          * ANSI standard says that |r.quot| < |n/d| (where n/d
  55.          * computed in infinite precision).  If the remainder is
  56.          * positive, we got the `wrong' answer, so fix it.
  57.          */
  58.         if (r.rem > 0) {
  59.             r.quot++;
  60.             r.rem -= denom;
  61.         }
  62.     }
  63.     return (r);
  64. }
  65.